<template>
  <ul class="todo-main">
    <Item v-for="item in todoList" 
    :key="item.id"
    v-bind="item" 
    />
  </ul>
</template>

<script>
import Item from "./Item";
import { v4 as uuidv4 } from "uuid";
export default {
  name: "List",
  components: {
    Item,
  },
  data() {
    return {
      todoList: [
        { id: uuidv4(), thing: "唱", done: false },
        { id: uuidv4(), thing: "跳", done: true },
        { id: uuidv4(), thing: "rap", done: false },
      ],
    };
  },
  mounted() {
    this.$bus.$on("newTodo", (newTodo) => {
      this.todoList.unshift(newTodo);
    });
    this.$bus.$emit("getTodos", this.todoList);
    this.$bus.$on("singleCheck",(id)=>{
      this.todoList.map(item=>{
        item.id===id?item.done=!item.done:item.done
      })
    })
  },
  watch: {
    todoList: {
      deep: true,
      handler() {
        this.$bus.$emit("getTodos", this.todoList);
      },
    },
  },
};
</script>

<style scoped>
/*main*/
.todo-main {
  margin-left: 0px;
  border: 1px solid #ddd;
  border-radius: 2px;
  padding: 0px;
}

.todo-empty {
  height: 40px;
  line-height: 40px;
  border: 1px solid #ddd;
  border-radius: 2px;
  padding-left: 5px;
  margin-top: 10px;
}
</style>
